package com.example.myapplication.ui.components

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.LocalIndication
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.example.myapplication.data.model.StorageSpace

/**
 * 储物空间行组件 - 用于详情页的子空间列表显示
 */
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun StorageSpaceRow(
    space: StorageSpace,
    onClick: () -> Unit,
    onLongClick: () -> Unit,
    modifier: Modifier = Modifier,
    showItemCount: Boolean = false,
    itemCount: Int = 0
) {
    val haptic = LocalHapticFeedback.current
    val interactionSource = remember { MutableInteractionSource() }
    val isPressed by interactionSource.collectIsPressedAsState()
    
    // 按压缩放动画
    val scale by animateFloatAsState(
        targetValue = if (isPressed) 0.98f else 1f,
        label = "row_scale"
    )
    
    Card(
        modifier = modifier
            .fillMaxWidth()
            .graphicsLayer {
                scaleX = scale
                scaleY = scale
            }
            .combinedClickable(
                interactionSource = interactionSource,
                indication = LocalIndication.current,
                onClick = {
                    println("DEBUG: StorageSpaceRow onClick called for: ${space.name}")
                    onClick()
                },
                onLongClick = {
                    println("DEBUG: StorageSpaceRow onLongClick called for: ${space.name}")
                    haptic.performHapticFeedback(HapticFeedbackType.LongPress)
                    onLongClick()
                }
            ),
        elevation = CardDefaults.cardElevation(defaultElevation = 2.dp)
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            // 空间图标 - 缩小并靠左
            Icon(
                imageVector = getStorageSpaceIcon(space.iconType),
                contentDescription = null,
                modifier = Modifier.size(24.dp),
                tint = MaterialTheme.colorScheme.primary
            )
            
            Spacer(modifier = Modifier.width(12.dp))
            
            // 空间信息
            Column(
                modifier = Modifier.weight(1f)
            ) {
                // 空间名称 - 与图标在同一行
                Row(
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(
                        text = space.name,
                        style = MaterialTheme.typography.titleMedium,
                        fontWeight = FontWeight.Medium,
                        maxLines = 1,
                        overflow = TextOverflow.Ellipsis
                    )
                }
                
                // 描述
                space.description?.let { description ->
                    Spacer(modifier = Modifier.height(4.dp))
                    Text(
                        text = description,
                        style = MaterialTheme.typography.bodyMedium,
                        color = MaterialTheme.colorScheme.onSurfaceVariant,
                        maxLines = 2,
                        overflow = TextOverflow.Ellipsis
                    )
                }
                
                // 物品数量（如果显示）
                if (showItemCount) {
                    Spacer(modifier = Modifier.height(4.dp))
                    Text(
                        text = if (itemCount > 0) "$itemCount 个物品" else "空",
                        style = MaterialTheme.typography.labelMedium,
                        color = MaterialTheme.colorScheme.primary
                    )
                }
            }
            
            // 子空间指示器
            Surface(
                color = MaterialTheme.colorScheme.primary.copy(alpha = 0.1f),
                shape = MaterialTheme.shapes.small
            ) {
                Row(
                    modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(
                        text = "子空间",
                        style = MaterialTheme.typography.labelSmall,
                        color = MaterialTheme.colorScheme.primary
                    )
                    Spacer(modifier = Modifier.width(4.dp))
                    Icon(
                        imageVector = Icons.Default.KeyboardArrowRight,
                        contentDescription = "进入",
                        modifier = Modifier.size(16.dp),
                        tint = MaterialTheme.colorScheme.primary
                    )
                }
            }
        }
    }
}

/**
 * 根据图标类型返回对应的图标
 */
@Composable
private fun getStorageSpaceIcon(iconType: String?): ImageVector = when (iconType) {
    "box" -> Icons.Default.Inventory2
    "folder" -> Icons.Default.Folder
    "closet" -> Icons.Default.Checkroom
    "bedroom" -> Icons.Default.BedroomParent
    "kitchen" -> Icons.Default.Kitchen
    "garage" -> Icons.Default.Garage
    "office" -> Icons.Default.Work
    "living_room" -> Icons.Default.Living
    "electronics" -> Icons.Default.Devices
    "books" -> Icons.Default.MenuBook
    "sports" -> Icons.Default.SportsBaseball
    "tools" -> Icons.Default.Build
    else -> Icons.Default.Inventory2 // 默认使用盒子图标
} 